package services

import (
	"fmt"
	"huaxrobot/tools"
	"net/http"
	"io/ioutil"
	"strings"
	"encoding/json"
)


var (
	URL = "http://10.100.19.138"
	PORT = "8081"
)

func MsRun(Id string) string {
	url := URL + ":" + PORT + "/performance/run"	
	sign := tools.MsSign()
	body :=fmt.Sprintf(` {"id":"%s","triggerMode":"MANUAL"}`,Id)
	fmt.Println("开始触发性能测试", url, "body:", body)
	client := &http.Client{}//客户端,被Get,Head以及Post使用
    reqest, err := http.NewRequest("POST", url, strings.NewReader(body))
	if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }
	reqest.Header.Set("Content-Type", "application/json") 
	reqest.Header.Set("accessKey", "XKBReyz2LNWIaUhV") 
	reqest.Header.Set("signature", sign) 
	resp, err := client.Do(reqest)//发送请求
	if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }
    defer resp.Body.Close()//一定要关闭resp.Body
    content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }	
    fmt.Println(string(content))
	var tmpresult map[string]interface{}
	json.Unmarshal(content, &tmpresult)
	reportId := tmpresult["data"].(string)
	fmt.Println("ppppppp:",reportId)
	return reportId
}



func MsGetStatus(Id string) string {
	url := URL + ":" + PORT + "/performance/report/" + Id	
	sign := tools.MsSign()	
	fmt.Println("查看性能测试状态", url)
	client := &http.Client{}//客户端,被Get,Head以及Post使用
    reqest, err := http.NewRequest("GET", url, strings.NewReader(""))
	if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }
	reqest.Header.Set("Content-Type", "application/json") 
	reqest.Header.Set("accessKey", "XKBReyz2LNWIaUhV") 
	reqest.Header.Set("signature", sign) 
	resp, err := client.Do(reqest)//发送请求
	if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }
    defer resp.Body.Close()//一定要关闭resp.Body
    content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }	
    //fmt.Println(string(content))
	var tmpresult map[string]interface{}
	json.Unmarshal(content, &tmpresult)
	status := tmpresult["data"].(map[string]interface{})["status"].(string)
	//fmt.Println("tttttttt:",status)
	return status
}



func MsGetReport(Id string) []byte {
	url := URL + ":" + PORT + "/performance/report/content/testoverview/" + Id 	
	sign := tools.MsSign()
	fmt.Println("获取测试结果", url)
	client := &http.Client{}//客户端,被Get,Head以及Post使用
    reqest, err := http.NewRequest("GET", url, strings.NewReader(""))
	if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }
	reqest.Header.Set("Content-Type", "application/json") 
	reqest.Header.Set("accessKey", "XKBReyz2LNWIaUhV") 
	reqest.Header.Set("signature", sign) 
	resp, err := client.Do(reqest)//发送请求
	if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }
    defer resp.Body.Close()//一定要关闭resp.Body
    content, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
    }	
    fmt.Println(string(content))

	return content
}